<?php


namespace App\Manage\Html;


class AdminList
{
    protected $module;
    protected $controller;

    // 列表页面
    public function __construct($module, $controller)
    {
        $this->module     = $module;
        $this->controller = $controller;
    }


    // 列表按钮


    // ajax搜索（选定单个）（可用）
    public function ajaxSingleFromList($field, $label, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {

        $showHtml = '<input type="hidden" name="'.$field.'"  id="search_'.$field.'"/>';

        // 展示输入框：带Ajax操作
        $fun  = strtolower($ajaxFun);
        $url  = url('/'.$this->module.'/selector/'.$fun);
        $html = '<input type="text" class="input-text valid" placeholder="请填写'.$label.'" id="show_'.$field.'_ID" style="width:145px;" onkeyup="ajaxSearch(\''.$url.'\', this, \''.$field.'\', \''.$show_id.'\', \''.$show_label.'\');" autocomplete="off" />';

        // 展示显示的值
        $html .= $showHtml;

        // 隐藏域 搜索后的下拉 与 下拉后的选择
        //$html .= '<input type="hidden" value="" name="'.$field.'" id="'.$field.'" />';
        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';

        return $html;
    }


    // 输入框
    public function formInput($field, $val, $label = '', $width = '336px')
    {
        $html = ' value="'.$val.'"   name="'.$field.'"   id="search_'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请填写'.$label.'"  ';
        }

        return '<input type="text" class="input-text" '.$html.'/>';
    }

    // 日期时间
    public function formDateTime($field, $val, $label = '', $type = 'date')
    {
        if($type == 'datetime'){
            $width  = '180px';
            $format = 'YYYY-MM-DD hh:mm:ss';
        } elseif($type == 'date'){
            $width  = '120px';
            $format = 'YYYY-MM-DD';
        } elseif($type == 'time'){
            $width  = '120px';
            $format = 'hh:mm:ss';
        }


        $html = ' value="'.$val.'"   name="'.$field.'"   id="search_'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请填写'.$label.'"  ';
        }

        // js
        $js = "<script type='text/javascript'>
        laydate.render({
            elem: '#search_{$field}'
            ,type: '{$type}'
          });
	    </script>";

        return '<input type="text" class="input-text" '.$html.'/>'.$js;
    }


    // 输入框：数字
    public function formNumber($field, $val, $width = '136px')
    {
        // <input type="number" min="1" onkeyup="this.value=this.value.replace(/\D/, '');" class="laypage_skip">
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';

        return '<input type="number" min="0" class="input-text" onkeyup="this.value=this.value.replace(/\D/, \'\');" class="laypage_skip" '.$html.'/>';
    }


    // 下拉框（二维数组）
    public function formSelectLists($field, $val, $lists, $val_field, $show_field)
    {
        $all  = '<option value="" >请选择...</option>';
        $html = ' <select onchange="$(\'#sComeFrom\').val($(this).val())" class="select" name="'.$field.'" style="width:55px;"> ';
        $html = ' <select class="input-text" name="'.$field.'"  id="search_'.$field.'" style="width:200px;" > ';
        foreach($lists as $key => $item){
            $selected = '';
            $_level   = '';
            if($val == $item[$val_field]){
                $selected = ' selected="selected" ';
            }
            if(!empty($item['level'])){
                $_level = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['level']).'|--';
            }
            $_show  = $_level.$item[$show_field];
            $_key   = $item[$val_field];
            $option = '<option value="'.$_key.'" '.$selected.'>'.$_show.'</option>';
            $all    .= $option;
        }

        return $html.$all.'</select>';
    }

    // 下拉框（一维数组）
    public function formSelect($field, $val, $data)
    {
        $all  = '<option value="" >请选择...</option>';
        $html = ' <select onchange="$(\'#sComeFrom\').val($(this).val())" class="select" name="'.$field.'" style="width:55px;"> ';
        $html = ' <select class="input-text" name="'.$field.'"  id="search_'.$field.'" style="width:155px;"> ';
        foreach($data as $key => $show){
            $selected = '';
            if(($key == $val) && (!is_null($val))){
                $selected = ' selected="selected" ';
            }
            $option = '<option value="'.$key.'" '.$selected.'>'.$show.'</option>';
            $all    .= $option;
        }

        return $html.$all.'</select>';
    }

    // 列表table
    public function list_check($td)
    {
        return '<td><input type="checkbox" value="'.$td.'" name="id" /></td>';
    }

    // 列表table
    public function list_number($field, $val, $id, $width = '70px')
    {
        // <input type="number" min="1" onkeyup="this.value=this.value.replace(/\D/, '');" class="laypage_skip">
        $url   = url('/'.$this->module.'/'.$this->controller.'/on-'.$field.'/id/'.$id);
        $html  = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';" ';
        $input = '<input type="number" min="0" class="input-text change-'.$field.'" onkeyup="this.value=this.value.replace(/\D/, \'\');" class="laypage_skip" '.$html.' data-url="'.$url.'" onChange="changeSort(this)" />';
        $str   = '<td>'.$input.'</td>';

        return $str;
    }

    // 列表table
    public function list_td($td, $align = '')
    {
        if($align){
            $str = '<td style="text-align: '.$align.'">'.$td.'</td>';
        } else{
            $str = '<td>'.$td.'</td>';
        }


        return $str;
    }

    // 列表table
    public function list_date($val, $type = 'datetime', $align = '')
    {
        $td = '-';
        if($val){
            if($type == 'date'){
                $td = date('Y-m-d', $val);
            } elseif($type == 'datetime'){
                $td = date('Y-m-d H:i:s', $val);
            } elseif($type == 'time'){
                $td = date('H:i:s', $val);
            }
        }

        if($align){
            $str = '<td style="text-align: '.$align.'">'.$td.'</td>';
        } else{
            $str = '<td>'.$td.'</td>';
        }

        return $str;
    }

    // 列表table
    public function list_image($td, $align = '')
    {
        $img = '';
        if($td){
            $img = '<img src="'.$td.'" width="100" height="50" >';
        }
        if($align){
            $str = '<td style="text-align: '.$align.'">'.$img.'</td>';
        } else{
            $str = '<td>'.$img.'</td>';
        }

        return $str;
    }

    // 列表table
    public function list_tr($tr)
    {
        $str = '<tr class="text-c">';
        //$str .= '<td><input type="checkbox" value="" name="id" /></td>';
        $str .= $tr;
        $str .= '</tr>';

        return $str;
    }

    // 操作：编辑
    public function operate_list($param, $title, $url)
    {
        $url = substr($url, 0, 1) == '/' ? substr($url, 1) : $url;
        $url = '/'.$this->module.'/'.$url;
        $com = '?';
        foreach($param as $field => $val){
            $url .= $com.$field.'='.$val;
            $com = '&';
        }
        $editUrl = url($url);

        //$editUrl = url('/innerend/admin/edit?id='.$item['id']);
        //return '<td onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</td>';
        return '<span class="btn btn-secondary radius" onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">'.$title.'</span>&nbsp;';
    }

    // 操作：编辑
    public function operate_edit($param, $title)
    {
        $url = '/'.$this->module.'/'.$this->controller.'/edit?';
        $com = '';
        foreach($param as $field => $val){
            $url .= $com.$field.'='.$val;
            $com = '&';
        }
        $editUrl = url($url);

        //$editUrl = url('/innerend/admin/edit?id='.$item['id']);
        //return '<td onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</td>';
        return '<span class="btn btn-primary radius" onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</span>&nbsp;';
    }

    // 操作：删除
    public function operate_delete($param, $msg = '你确定要删除该记录吗？')
    {
        $url = '/'.$this->module.'/'.$this->controller.'/on-delete?';
        $com = '';
        foreach($param as $field => $val){
            $url .= $com.$field.'='.$val;
            $com = '&';
        }
        $editUrl = url($url);

        //$editUrl = url('/innerend/admin/edit?id='.$item['id']);
        //return '<td onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</td>';
        return '<span class="btn btn-danger radius" onclick="onDelete(\''.$editUrl.'\',\''.$msg.'\')">删除</span>&nbsp;';
    }


    // 操作：删除
    public function operate_deal($url, $param, $label, $msg = '你确定要该记录吗？')
    {
        $url = '/'.$this->module.'/'.$this->controller.'/'.$url.'/?';
        $com = '';
        foreach($param as $field => $val){
            $url .= $com.$field.'='.$val;
            $com = '&';
        }
        $editUrl = url($url);

        //$editUrl = url('/innerend/admin/edit?id='.$item['id']);
        //return '<td onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</td>';
        return '<span class="btn btn-secondary radius" onclick="onDelete(\''.$editUrl.'\',\''.$msg.'\')">'.$label.'</span>&nbsp;';
    }


    // 操作：删除
    public function operate_soft_delete($param, $msg = '你确定要删除该记录吗？')
    {
        $url = '/'.$this->module.'/'.$this->controller.'/on-soft?';
        $com = '';
        foreach($param as $field => $val){
            $url .= $com.$field.'='.$val;
            $com = '&';
        }
        $editUrl = url($url);

        //$editUrl = url('/innerend/admin/edit?id='.$item['id']);
        //return '<td onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</td>';
        return '<span class="btn btn-danger radius" onclick="onDelete(\''.$editUrl.'\',\''.$msg.'\')">删除</span>&nbsp;';
    }

    // 操作：状态
    public function operate_status($param, $title, $type = 1, $on_operate = 'on-status')
    {
        //$statusUrl = url('/innerend/admin/on-status?id='.$item['id']."&status=".(1 - $item['status']));
        $url = '/'.$this->module.'/'.$this->controller.'/'.$on_operate.'?';
        $com = '';
        foreach($param as $field => $val){
            $url .= $com.$field.'='.$val;
            $com = '&';
        }
        $editUrl = url($url);

        // secondary disabled
        //$editUrl = url('/innerend/admin/edit?id='.$item['id']);
        //return '<td onclick="onEdit(\''.$editUrl.'\',\''.$title.'\')">编辑</td>';
        $class = $type == 1 ? "btn btn-disabled radius" : "btn btn-secondary radius";

        return '<span class="'.$class.'" onclick="changeStatus(\''.$editUrl.'\')">'.$title.'</span>&nbsp;';
    }


}


